package com.example.kugou.commons.file;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class FindStringInTxt {
    String encoding = "UTF-8";
    public static void main(String[] args) throws IOException {
        chenge("C:\\Users\\Administrator\\Desktop\\新建文件夹\\新建文件夹");
    }

    public static List<String> findStringInFile(File file, String name) throws IOException {
        List<String> strings = new ArrayList<>();
        InputStreamReader read = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);//考虑到编码格式
        BufferedReader bufferedReader = new BufferedReader(read);
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            if(line.startsWith("#")){
                continue;
            }
            //指定字符串判断处
            if (line.contains(name) && (line != null && !line.equals(""))) {
                strings.add(line);
            }
        }
        return strings;
    }



    public static List<List<String>> get(File file,String name) {
        List<List<String>> lists = new ArrayList<>();
        List<String> strings = new ArrayList<>();
        try {
            //判断文件是否是文件，如果是文件，获取路径，并计数
            if (file.isFile()) {
                String s = file.getPath();
                if (s.endsWith("lrc")) {
                        File newfiles = new File(s);
                    try {
                        strings = findStringInFile(newfiles,name);
                        lists.add(strings);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                //如果是文件夹，声明一个数组放文件夹和他的子文件
                File[] f = file.listFiles();
                //遍历文件件下的文件，并获取路径
                for (File file2 : f) {
                  lists.addAll(get(file2,name));
                }
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
        return lists;
    }

    public static void chenge(String fileName) {
        List<String> strings = new ArrayList<>();
        File file = new File(fileName);
        try {
            //判断文件是否是文件，如果是文件，获取路径，并计数
            if (file.isFile()) {
                String s = file.getPath();
                if (s.endsWith("lrc")) {
                    File newfiles = new File(s);
                    try {
                        strings = findStringInFile(newfiles,"");
                        File newFile = new File(fileName.substring(0,fileName.lastIndexOf(".")) + ".json");
                        chengeType(newFile,strings);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                //如果是文件夹，声明一个数组放文件夹和他的子文件
                File[] f = file.listFiles();
                //遍历文件件下的文件，并获取路径
                for (File file2 : f) {
                    chenge(file2.getAbsolutePath());
                }
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }

    /**
     * 更改歌词的格式
     * @param makeBoatsLog
     * @param strings
     */
    public static void chengeType(File makeBoatsLog,List<String> strings){
        FileWriter fw;
        StringBuilder string = new StringBuilder("[");
        try {
            fw=new FileWriter(makeBoatsLog,true);
            String lineEnd="\r\n";
            for (int i = 1; i < strings.size();i++){
                string.append("\"" + strings.get(i) + "\"," +lineEnd );
            }
            fw.write(string+"]");
            fw.flush();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
